//
//  ZCMeNotificationCenterVC.m
//  zctx
//
//  Created by apple on 15/11/5.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeNotificationCenterVC.h"
#import "ZCMeNotificationCenterCell.h"
#import "ZCMeAuditDetailsVC.h"  //审核中
#import "ZCSamplingDetailVC.h"  //抽样中
#import "ZCInvolvedDetailsVC.h"  //众筹中
#import "ZCProjectdetailsVC.h"  //结果

#import "DZWebViewController.h"

@interface ZCMeNotificationCenterVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation ZCMeNotificationCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    [self.navigationItem setTitle:@"通知中心"];
    self.dataArray = [NSMutableArray array];
    [self initnavegation];
    [self initTableView];
    [self requestSeeResultdata];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCMeNotificationCenterCell" bundle:nil] forCellReuseIdentifier:@"centercell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 请求结果数据
-(void)requestSeeResultdata{
    
    ProgressHUDStr;
    [NetworkManager requestGetMyNoticeWithMid:UserModel.mid password:UserModel.password success:^(NSArray *ArrayResponse) {
        ProgressDismiss;
        self.dataArray = [ArrayResponse mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
     
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellid =@"centercell";
    ZCMeNotificationCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[ZCMeNotificationCenterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.row];
    cell.titleLable.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]];
    cell.contentLable.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"text"]];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.row];
    NSString *type =[NSString stringWithFormat:@"%@",[dict objectForKey:@"showtype"]];
    NSString *state =[NSString stringWithFormat:@"%@",[dict objectForKey:@"fd_state"]];
    NSString *fdid =[NSString stringWithFormat:@"%@",[dict objectForKey:@"fdid"]];
    NSString *title =[NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]];
    NSString *content =[NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];

//    NSLog(@"type = %@ , %@ ,%@ ",type,state,fdid);
    if ([type isEqualToString:@"1"]) {
        //项目消息
        if ([state isEqualToString:@"0"])
        { // 0 审核中 1众筹中 2 抽样中 3完成 4 异常
           
            ZCMeAuditDetailsVC *vc = [[ZCMeAuditDetailsVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([state isEqualToString:@"1"]){
            ZCInvolvedDetailsVC *vc = [[ZCInvolvedDetailsVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([state isEqualToString:@"2"]){
            ZCSamplingDetailVC *vc = [[ZCSamplingDetailVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([state isEqualToString:@"3"]){
            ZCProjectdetailsVC *vc = [[ZCProjectdetailsVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        //系统消息
        DZWebViewController *webvc = [[DZWebViewController alloc]init];
        webvc.strURL = content;
        webvc.title = title;
        webvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webvc animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
